package com.dbexposer.ws.rest;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.Context;
import org.mybatis.generator.config.JDBCConnectionConfiguration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import com.dbexposer.db.mybatis.generator.GeneratorInterceptorPlugin;
import com.dbexposer.db.mybatis.generator.GeneratorInterceptorPlugin.GeneratorInfo;
import com.dbexposer.ws.ServiceGeneratorConfig;

public class MyBatisServiceGenerator {
 
	private ServiceGeneratorConfig config;
	private File mbgConfigFile;

	public MyBatisServiceGenerator() {
		// TODO Auto-generated constructor stub
	}
	
	// FIXME GROUP PARAMETERS INTO A NEW CONFIG OBJECT
	public void initialize(ServiceGeneratorConfig config) {
		this.config = config;
		this.mbgConfigFile = generatePersistenceEngineConfig();
	}
	
	public void generate() {
		Map<String, GeneratorInfo> myBatisCodeInfo = generatePersistenceEngineCode();
		// FIXME TRANSLATE INTO VELOCITY VARIABLES, EXCLUDING NOT NEEDED METHODS
		
	}
	
	private File generatePersistenceEngineConfig() {
		// FIXME CREATE MYBATISGEN XML CONFIG FROM ServiceGeneratorConfig
		// FIXME TEST MYBATISGEN 
		URL mbgConfigURL = MyBatisServiceGenerator.class.getClassLoader().getResource("test-mybatis-generator-conf.xml");
		File mbgConfigFile = null;
		try {
			mbgConfigFile = new File(mbgConfigURL.toURI());
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		// FIXME END TEST MYBATISGEN
		
		return mbgConfigFile;
	}
	
	// Returns MyBatis generated code metainfo
	// FIXME CHANGE RETURN TYPE FOR ANOTHER VALID FOR HIBERNATE, JPA...
	private Map<String, GeneratorInfo> generatePersistenceEngineCode() {
		
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration mbgConfig = null;
		try {
			mbgConfig = cp.parseConfiguration(mbgConfigFile);
		} catch (Exception e) {
			// App internal error, should never happen
			throw new RuntimeException(e);
		}

		Context context = mbgConfig.getContext("Tables");
		
		JDBCConnectionConfiguration jdbcConfig = context.getJdbcConnectionConfiguration();
		
		jdbcConfig.setDriverClass(config.getDbDriver());
		jdbcConfig.setConnectionURL(config.getDbURL());
		jdbcConfig.setUserId(config.getDbUser());
		jdbcConfig.setPassword(config.getDbPassword());
		
		context.setJdbcConnectionConfiguration(jdbcConfig);
		
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator;
		try {
			myBatisGenerator = new MyBatisGenerator(mbgConfig, callback, warnings);
			myBatisGenerator.generate(null);
		} catch (Exception e) {
			// App internal error, should never happen
			throw new RuntimeException(e);
		}
		
		return GeneratorInterceptorPlugin.getInfosByTable();
	}
	
}
